﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace CustomerService
{
    public class LabeledText : System.Web.UI.WebControls.TextBox
    {
        private string _label = string.Empty;
        private bool _small = false;
        private bool _error = false;

        public LabeledText()
        {
        }

        public string Label
        {
            get { return _label; }
            set { _label = value; }
        }

        public bool Small
        {
            get { return _small; }
            set { _small = value; }
        }

        public bool Error
        {
            get { return _error; }
            set { _error = value; }
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            if (!_small)
            {
                writer.Write(_label + "<br />");
            }
            base.RenderControl(writer);
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            if (_small)
            {
                writer.AddAttribute("onfocus", "text_onfocus(this, '" + _label + "');");
                writer.AddAttribute("onblur", "text_onblur(this, '" + _label + "');");
                if (Text == string.Empty)
                {
                    writer.AddAttribute("value", _label);
                    writer.AddAttribute("class", ((_error) ? "invalidlabel" : "label"));
                }
                else
                    writer.AddAttribute("class", ((_error) ? "invalid" : ""));
            }
            base.AddAttributesToRender(writer);
        }

        public override string Text
        {
            get
            {
                if (base.Text == _label)
                    return string.Empty;
                else
                    return base.Text;
            }
            set
            {
                base.Text = value;
            }
        }
    }
}
